package com.example.shop.fill;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.example.shop.pojo.Evaluation;
import com.example.shop.pojo.Merchant;
import com.example.shop.pojo.User;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.time.LocalDate;
import java.util.Date;

@Component
public class AutoFill implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        Object originalObject = metaObject.getOriginalObject();
        //如果插入的对象是一个用户对象，则进行下面填充操作
        if(originalObject instanceof User){
            User user = (User) originalObject;
            if(StringUtils.isEmpty(user.getUserSignature())){
                this.setFieldValByName("userSignature","此人很懒,没留下什么!",metaObject);
            }
            if(StringUtils.isEmpty(user.getUserName())){
                this.setFieldValByName("userName",user.getUserId()+"用户",metaObject);
            }
            if(StringUtils.isEmpty(user.getUserPassword())){
                this.setFieldValByName("userPassword","123456",metaObject);
            }
            //插入一个用户时，会自动将当前的时间，作为该用户的注册时间
            this.setFieldValByName("userRegisterDate", LocalDate.now(),metaObject);
        }
        if(originalObject instanceof Merchant){
            Merchant merchant = (Merchant) originalObject;
            if(StringUtils.isEmpty(merchant.getMerchantName())){
                this.setFieldValByName("merchantName",merchant.getMerchantId()+"商家",metaObject);
            }
            if(StringUtils.isEmpty(merchant.getMerchantPassword())){
                this.setFieldValByName("merchantPassword","123456",metaObject);
            }
            //插入一个用户时，会自动将当前的时间，作为该用户的注册时间
            this.setFieldValByName("merchantRegisterDate", LocalDate.now(),metaObject);
        }
        if(originalObject instanceof Evaluation){
            Evaluation evaluation = (Evaluation) originalObject;
            if(StringUtils.isEmpty(evaluation.getEvaluationContent())){
                this.setFieldValByName("evaluation_content","该用户觉得商品很好，给出了好评",metaObject);
            }
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Object originalObject = metaObject.getOriginalObject();
        //如果插入的对象是一个用户对象，则进行下面填充操作
        if(originalObject instanceof User){
            User user = (User) originalObject;
            if(StringUtils.isEmpty(user.getUserSignature())){
                this.setFieldValByName("userSignature","此人很懒,没留下什么!",metaObject);
            }
            if(StringUtils.isEmpty(user.getUserName())){
                this.setFieldValByName("userName",user.getUserId()+"用户",metaObject);
            }
            if(StringUtils.isEmpty(user.getUserArea())){
                this.setFieldValByName("userArea",null,metaObject);
            }
        }
    }


}
